package com.chj.comp.common.mqadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chj.comp.common.core.util.Result;
import com.chj.comp.common.core.entity.MqMessageInfo;
import com.chj.comp.common.mqadmin.mapper.MqMessageInfoMapper;
import com.chj.comp.common.mqadmin.service.IMqMessageInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chj.comp.common.rabbitmq.constant.MsgConstant;
import com.chj.comp.common.rabbitmq.constant.MsgStatusEnum;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * rabbitmq消息信息表 服务实现类
 * </p>
 *
 * @author cuihui
 * @since 2022-06-14
 */
@Service
public class MqMessageInfoServiceImpl extends ServiceImpl<MqMessageInfoMapper, MqMessageInfo> implements IMqMessageInfoService {

    @Override
    public Result addMqMessageInfo(MqMessageInfo mqMessageInfo) {
        mqMessageInfo.setCreateTime(LocalDateTime.now());
        mqMessageInfo.setUpdateTime(LocalDateTime.now());
        this.save(mqMessageInfo);
        return Result.ok("保存成功");
    }

    @Override
    public Result updateMqMessageStatus(String messageId, String status) {
        MqMessageInfo mqMessageInfo = getMqMessageByMsgId(messageId);
        this.updateById(MqMessageInfo.builder().id(mqMessageInfo.getId())
                .status(status)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.ok("更新成功");
    }

    @Override
    public Result<Integer> judgeMqMessageStatus(String messageId) {
        LambdaQueryWrapper<MqMessageInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MqMessageInfo::getMessageId, messageId).eq(MqMessageInfo::getStatus, MsgStatusEnum.STATUS_3.getStatus());
        int count = this.count(lambdaQueryWrapper);
        return Result.ok(Integer.valueOf(count));
    }

    @Override
    public List<MqMessageInfo> getErrorMqMessageList() {
        // 获取失败消息列表：1、未消费 2、重试次数<3 3、未过期
        LambdaQueryWrapper<MqMessageInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(MqMessageInfo::getStatus, MsgStatusEnum.STATUS_3.getStatus())
                          .lt(MqMessageInfo::getSendNum, MsgConstant.MAX_SEND_NUM)
                          .gt(MqMessageInfo::getCreateTime, LocalDateTime.now().minusMinutes(MsgConstant.EXPIRE_TIME));
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public Result addMqMessageSendNum(String messageId) {
        MqMessageInfo mqMessageInfo = getMqMessageByMsgId(messageId);
        this.updateById(MqMessageInfo.builder().id(mqMessageInfo.getId())
                .sendNum(mqMessageInfo.getSendNum()+1)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.ok("更新成功");
    }

    /**
     * 根据消息id获取消息
     * @param messageId
     * @return
     */
    private MqMessageInfo getMqMessageByMsgId(String messageId){
        LambdaQueryWrapper<MqMessageInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        MqMessageInfo mqMessageInfo = this.getOne(lambdaQueryWrapper.eq(MqMessageInfo::getMessageId, messageId));
        return mqMessageInfo;
    }
}
